﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using YiHaoHospitalPlatform;
using YiHaoHospitalPlatform.Model;

namespace YiHaoHospitalPlatformDemo
{
    /// <summary>
    /// DeptmentWindow.xaml 的交互逻辑
    /// </summary>
    public partial class DepartmentWindow : Window
    {
        public Action GetWebAction = new Action(QueryData);
        public DepartmentWindow()
        {
            InitializeComponent();
        }
        #region 窗体基本控制
        /// <summary>
        /// 鼠标左键按下拖动窗口
        /// </summary>
        /// <param name="sender">事件发送者</param>
        /// <param name="e">路由事件</param>
        private void Title_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }
        /// <summary>
        /// 最小化
        /// </summary>
        /// <param name="sender">消息发送者</param>
        /// <param name="e">路由事件</param>
        private void min_button_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender">消息发送者</param>
        /// <param name="e">路由事件</param>
        private void close_button_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
        #endregion

        private void submit_button_Click(object sender, RoutedEventArgs e)
        {
            if (code_string.Text.Length == 0 || name_string.Text.Length == 0 )
                return;
            List<Department> list = new List<Department>();
            Department department = new Department() { DepartmentId = code_string.Text, DepartmentName = name_string.Text };
            list.Add(department);
            CommitDepartmentInstance.Instance.setQueryDepartmentAction(GetWebAction).StartCommitDepartments(list);
        }
        private static void QueryData()
        {
            List<Department> list = new List<Department>();
            for (int i = 1; i <= 10; i++)
            {
                Department department = new Department();
                department.DepartmentId = GetRandomString(i, i % 2 == 0, i % 2 != 0, i % 2 == 0, false, "");
                department.DepartmentName = GetRandomString(i, i % 2 == 0, i % 2 != 0, i % 2 == 0, false, "");
                list.Add(department);
            }
            CommitDepartmentInstance.Instance.StartCommitDepartments(list);
        }


        ///<summary>
        ///生成随机字符串 
        ///</summary>
        ///<param name="length">目标字符串的长度</param>
        ///<param name="useNum">是否包含数字，1=包含，默认为包含</param>
        ///<param name="useLow">是否包含小写字母，1=包含，默认为包含</param>
        ///<param name="useUpp">是否包含大写字母，1=包含，默认为包含</param>
        ///<param name="useSpe">是否包含特殊字符，1=包含，默认为不包含</param>
        ///<param name="custom">要包含的自定义字符，直接输入要包含的字符列表</param>
        ///<returns>指定长度的随机字符串</returns>
        public static string GetRandomString(int length, bool useNum, bool useLow, bool useUpp, bool useSpe, string custom)
        {
            byte[] b = new byte[4];
            new System.Security.Cryptography.RNGCryptoServiceProvider().GetBytes(b);
            Random r = new Random(BitConverter.ToInt32(b, 0));
            string s = null, str = custom;
            if (useNum == true) { str += "0123456789"; }
            if (useLow == true) { str += "abcdefghijklmnopqrstuvwxyz"; }
            if (useUpp == true) { str += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; }
            if (useSpe == true) { str += "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; }
            for (int i = 0; i < length; i++)
            {
                s += str.Substring(r.Next(0, str.Length - 1), 1);
            }
            return s;
        }
    }
}
